package com.ssm.studen.service.impl;

import com.ssm.studen.dao.TeacherDao;
import com.ssm.studen.model.Teacher;
import com.ssm.studen.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 教师表：
 *      实现业务及流程接口当中的内容
 */
@Service
public class TeacherServiceImpl implements TeacherService {

    @Autowired
    private TeacherDao teacherDao;

    /**
     * 教师添加方法
     * @param teacher
     * @return
     */
    @Override
    public int addTeacher(Teacher teacher) {
        int returnInt = 0;
        /*
         * 业务判断：
         *       教师号不能重复
         * */
        int teacherIdCount = teacherDao.getTeacherIdCount(teacher.getTid());
        if(teacherIdCount<1){
            //2.数据添加
            int addTeacherInt = teacherDao.insertTeacher(teacher);
            //3.返回值设置
            returnInt = addTeacherInt;
        }else{
            //有相同的角色名
            returnInt = -1;
        }
        return returnInt;
    }


    /**
     * 教师删除方法
     * @param tid
     * @return
     */
    @Override
    public int delTeacher(String tid) {
        int delTeacher = 0;
        try {
            //返回的结果总数量小于1，才可以删除
            if(teacherDao.getTeacherIdCount(tid)<1){
                delTeacher = teacherDao.delectTeacher(tid);
            }
        }catch (Exception e){
            delTeacher = -1;
            System.out.println("删除角色信息时执行SQL报错，信息为：" + e.getMessage());
        }
        return delTeacher;
    }


    /**
     * 教师更新方法
     * @param teacher
     * @return
     */
    @Override
    public int updateTeacher(Teacher teacher) {
        int isUpdate = 0;
        try {
            isUpdate = teacherDao.updateTeacher(teacher);
        }catch (Exception e){
            isUpdate = -1;
            //打印更新时报错的情况
            System.out.println("更新学生信息时执行SQL报错的原因是：" + e.getMessage());
        }
        return isUpdate;
    }


    /**
     * 教师查询方法
     */
}
